﻿using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using Google.Api.Maps.Service.StaticMaps;
using SQLLink.DomainClasses;

namespace Localizer
{
    /// <summary>
    /// GooglePictureBox
    /// This class derives from a PictureBox, so it contains all of its properties
    /// Plus, it contains more properties proper to a googlePictureBox object
    /// </summary>
    class GooglePictureBox : PictureBox
    {
        private const int MARKERS_MAX_FOR_GOOGLE_API_REQUEST = 25;  //The maximum number of markers google API will still respond
        private StaticMap map = new StaticMap();                    //Map attributes
       
        /// <summary>
        /// This method gets the map in the gps mode with the user selected markers
        /// </summary>
        /// <param name="markers">List of markers to display on the map</param>
        public string GetMap(List<Marker> markers)
        {
            map.Markers = null;                 // Deleting the markers
            map.Size = Width + "x" + Height;    // Size of the map : Division by 2 is to adjust the scale
            map.Sensor = "false";               // No sensor for the map
            map.Language = "fr";                // Map language is french
            map.MapType = "roadmap";            // Maptype is roadmap

            var ImageLocationStr = map.ToUri() + @"&scale=2"; // Start creation of the image location uri

            // Retrieves the others, the owner, the departure, the arrival and the busses in different IEnumerable<Marker>
            var others = markers.FindAll(x => x.Type == MarkerType.other);
            var owner = markers.Find(x => x.Type == MarkerType.owner);
            var departure = markers.Find(x => x.Type == MarkerType.departure) ?? owner;
            var arrival = markers.Find(x => x.Type == MarkerType.arrival) ?? owner;
            var busses = markers.FindAll(x => x.Type == MarkerType.bus);

            // Adds the departure uri to the image location uri if the owner is not the departure
            if(owner != departure)
                ImageLocationStr += "&markers=" + "color:" + departure.Color.ToKnownColor().ToString().ToLower() + "|" + departure.LatLon;

            // Adds the arrival uri to the image location uri if the owner is not the arrival
            if (owner != arrival)
                ImageLocationStr += "&markers=" + "color:" + arrival.Color.ToKnownColor().ToString().ToLower() + "|" + arrival.LatLon;

            // Adds the owner marker
            ImageLocationStr += "&markers=" + "color:" + owner.Color.ToKnownColor().ToString().ToLower() + "|" + owner.LatLon;

            // Adds the busses markers only if there are any
            if (busses.Count > 0)
                ImageLocationStr += "&markers=" + "color:" + busses.First().Color.ToKnownColor().ToString().ToLower();
            foreach (var bus in busses)
            {
                ImageLocationStr += "|" + bus.LatLon;
            }

            // Adds the other bus stops markers
            if (others.Count > 0)
                ImageLocationStr += "&markers=size:tiny|color:black";
            foreach (var other in others)
            {
                if (others.IndexOf(other) < MARKERS_MAX_FOR_GOOGLE_API_REQUEST)
                    ImageLocationStr += "|" + other.LatLon;
            }
            
            return ImageLocationStr.Remove(ImageLocationStr.Length - 1); // Loads the image
        }
    }
}